package com.example.studyonlineapplication.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.studyonlineapplication.R;
import com.example.studyonlineapplication.domain.po.Note;
import com.example.studyonlineapplication.domain.po.Result;
import com.example.studyonlineapplication.domain.po.User;
import com.example.studyonlineapplication.utils.MyToast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NoteTabFragment extends Fragment {
    int state;
    String searchSt;
    OkHttpClient client = new OkHttpClient();
    Request request;
    String URL = "http://10.0.2.2:8080/note/getNoteAndCourse";
    Result<List<Note>> data;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        state = getArguments().getInt("state");
        searchSt = getArguments().getString("seCond");
        if (searchSt==null){
            searchSt = "";
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.none_page,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        System.out.println("创建了tab");
        super.onViewCreated(view, savedInstanceState);
        Gson gson = new Gson();
//        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
        SharedPreferences sp = getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        User user = gson.fromJson(sp.getString("user",""), User.class);
        HttpUrl url = HttpUrl.parse(URL)
                .newBuilder()
                .addQueryParameter("id",String.valueOf(user.getId()))
                .build();
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        client.newCall(request).enqueue(new Callback(){
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String json = response.body().string();
                System.out.println(json);
                Type resultType = new TypeToken<Result<List<Note>>>() {}.getType(); // 定义 Result<User> 的类型
                data = gson.fromJson(json, resultType);
                getActivity().runOnUiThread(()->{
                    if(data.getCode()==1){
                        MyToast.message(data.getMessage(),getContext());
                    }else {
                        FragmentManager fragmentManager = getChildFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();


                        for(Note note : data.getData()){
//                            System.out.println(LocalDateTime.parse(note.getUpdatetime()).getDayOfMonth()+","+LocalDateTime.now().getDayOfMonth());
                            boolean con = (state == 0 && LocalDateTime.parse(note.getUpdatetime()).getDayOfMonth()!=LocalDateTime.now().getDayOfMonth())
                                    || (!note.getNoteText().contains(searchSt));
//                                    || (!note.getNoteText().equals(searchSt));

                            if (con){
                                continue;
                            }
                            NoteEntityFragment noteEntityFragment = new NoteEntityFragment();

                            fragmentTransaction.add(R.id.entityContainer,noteEntityFragment);
                            Bundle bundle = new Bundle();
                            bundle.putString("username", user.getUsername());
                            bundle.putInt("noteId",note.getNoteId());
                            bundle.putInt("nodeId",note.getNoteId());
                            bundle.putString("noteText",note.getNoteText());
                            bundle.putString("noteTime",note.getNoteTime());
                            bundle.putString("courseName",note.getCourseName());
                            bundle.putString("createtime",note.getCreatetime().toString());
                            bundle.putString("updatetime",note.getUpdatetime().toString());
                            noteEntityFragment.setArguments(bundle);
                        }
                        fragmentTransaction.add(R.id.entityContainer,new BlankViewFragment());

                        fragmentTransaction.commit();

                    }
                });
            }

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                System.out.println("no 不");
            }
        });


    }
}
